<?php

class security extends Base_model
{
	function __Construct()
	{
        parent::Model();
        parent::__construct();
		
		$this->load->model("aplikasi");	
		$this->load->model("aplikasi_role");
		
	}
	
	function is_admin()
	{
		if($this->session->userdata("username")=='admin' || $this->session->userdata("id_role")==1)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	
	function has_access($app_id=null,$role_id=null,$action=false)
	{
		//exclude system page
		$exclude = array("noentry","logout","login","home");		
		if($this->session->userdata("username")!="")
		{
	
			if($app_id==null)
			{
				//jika app_id null maka coba cek berdasarkan path
				//
				$current_app = $this->class;
				$current_path = $this->path;
				if(strlen($this->dir)>0){
					$current_app = str_replace("/","",$this->dir);
				}
							
				//cari id dari current app
				$cp = $this->aplikasi->get_by_name($current_app);
				$admin = $this->is_admin();
			}
			else
			{
				$cp = $this->aplikasi->get_by_id($app_id);
				$br = $this->aplikasi_role->is_exists($role_id,$app_id);				
				$admin = $this->is_admin();
			}
			
			if($br || $admin)
			{
				return TRUE;
			}
			else
			{				
				if($action){
					$this->no_entry();
				}
				
				return false;
			}
		
		}
	}
}

?>